<?php
App::uses('AppModel', 'Model');
/**
 * Item Model
 *
 * @property LogLine $LogLine
 * @property Character $Character
 * @property Trait $Trait
 */
class Item extends AppModel {
	var $actsAs = array('SoftDeletable' => array('find' => true));

/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'name' => array(
			'notEmpty' => array(
				'rule' => array('notEmpty'),
				'message' => 'All items must have a name',
			),
		),
// TODO: Fix validation for Status
		'status' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'expiry' => array(
			'date' => array(
				'rule' => 'date',
				'message' => 'Enter a valid date',
				'allowEmpty' => true
			),
		),
		'description' => array(),
		'player_text' => array(),
		'cs_text' => array(),
		'deleted' => array(
			'boolean' => array(
				'rule' => array('boolean'),
				'message' => 'You should not be seeing this message. Deleted should be true or false.',
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * hasMany associations
 *
 * @var array
 */
	public $hasMany = array(
		'LogLine' => array(
			'className' => 'LogLine',
			'foreignKey' => 'item_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);


/**
 * hasAndBelongsToMany associations
 *
 * @var array
 */
	public $hasAndBelongsToMany = array(
		'Character' => array(
			'className' => 'Character',
			'joinTable' => 'characters_items',
			'foreignKey' => 'item_id',
			'associationForeignKey' => 'character_id',
			'unique' => 'keepExisting',
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
			'deleteQuery' => '',
			'insertQuery' => ''
		),
		'Trait' => array(
			'className' => 'Trait',
			'joinTable' => 'items_traits',
			'foreignKey' => 'item_id',
			'associationForeignKey' => 'trait_id',
			'unique' => 'keepExisting',
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
			'deleteQuery' => '',
			'insertQuery' => ''
		)
	);

/**
 * Builds search conditions for Players.
 * Function has knowledge of different search types for this model.
 * Search types are used in the view by the call to QuickSearchHelper.
 *
 * @param array with search conditions $data
 * @return multitype: array with CakePHP search conditions
 */
	function buildConditions($data) {
		if(array_key_exists('Search', $data)) {
			$data = $data['Search'];
			$type = $data['searchType'];
			$value = $data['searchValue'];
		} else {
			$type = -1;
		}

		switch ($type) {
			case 0 :
				$conditions = array('Item.id LIKE' => "%$value%");
				break;
			case 1 :
				$conditions = array('Item.name LIKE' => "%$value%");
				break;
			case 2 :
				$conditions = array('Item.status LIKE' => "%$value%");
				break;
			case 3 :
				$conditions = array('Item.expiry <=' => "$value");
				break;
			case 4 :
				$conditions = array('Item.expiry >=' => "$value");
				break;
			case 5 :
				$conditions = array('Item.description LIKE' => "%$value%");
				break;
			case 6 :
				$conditions = array(
					"OR" => array(
						'Item.name LIKE' => "%$value%",
						'Item.description LIKE' => "%$value%",
					)
				);
				break;
			default :
				$conditions = array();
		}

		return $conditions;
	}

	/**
	 * Prevents soft-deleted records from turning up.
	 * @see Model::beforeFind()
	 */
//	function beforeFind($queryData) {
//		$queryData['conditions']['Item.deleted'] = 0;
//		return $queryData;
//	}

	/**
	 * Prevents records from being deleted and instead soft-deletes them.
	 * @see Model::beforeDelete()
	 */
//	function beforeDelete($cascade) {
//		$this->saveField('deleted', 1, false);
//		return false;

//		//TODO fix Cascade
//	}
}
